<template>
  <a-modal
    title="填写物流"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top:20px;"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="物流公司">
          <a-input placeholder="请输入物流公司" v-decorator="['deliveryCompany', validatorRules.deliveryCompany ]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="物流单号">
          <a-input placeholder="请输入物流单号" v-decorator="['deliverySn', validatorRules.deliverySn ]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { httpAction } from '@/api/manage'
  import { getAction } from '@/api/manage'
  import CountDown from '@/components/CountDown/CountDown'

  export default {
    name: "OmsOrderEditAddressModal",
    components: {
      CountDown,
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        model: {},
        areaData: [],
        areaValue:[],
        endTime: Number,
        validatorRules: {
          deliveryCompany:{ rules: [{ required: true, message: '请输入物流公司!' }] },
          deliverySn: { rules: [{ required: true, message: '请输入物流单号!' }] }
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form:this.$form.createForm(this),
        url: {
          edit: "/oms/omsOrder/editAddress",
          areaJson: "/data/area",
        },
      }
    },
    created () {
      console.log("created");
    },
    methods: {
      onChange(value, selectedOptions) {
        console.log(value, selectedOptions);
        this.model.receiverProvinceCode = selectedOptions[0].value;
        this.model.receiverCityCode = selectedOptions[1].value;
        this.model.receiverRegionCode = selectedOptions[2].value;
        this.model.receiverProvince = selectedOptions[0].label;
        this.model.receiverCity = selectedOptions[1].label;
        this.model.receiverRegion = selectedOptions[2].label;
        this.areaValue = [this.model.receiverProvinceCode,this.model.receiverCityCode,this.model.receiverRegionCode];
      },
      show(record) {
        //this.form.resetFields();
        this.getArea();
        this.endTime = new Date().getTime() + 300000;
        this.visible = true;
        this.model = record;
        this.areaValue = [this.model.receiverProvinceCode,this.model.receiverCityCode,this.model.receiverRegionCode];
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'receiverName','receiverPhone','receiverDetailAddress','deliveryCompany','deliverySn'))
        });
      },
      getArea() {
        getAction(this.url.areaJson).then((res) => {
          this.areaData = res;
        })
      },
      close () {
        this.form.resetFields();
        this.$emit('close');
        this.visible = false;
      },
      handleSubmit () {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            console.log(values)
            this.confirmLoading = true;
            //let formData = Object.assign(this.model, values);
            let formData = Object.assign(values, {id:this.model.id,receiverProvinceCode:this.model.receiverProvinceCode,receiverCityCode:this.model.receiverCityCode,receiverRegionCode:this.model.receiverRegionCode,receiverProvince:this.model.receiverProvince,receiverCity:this.model.receiverCity,receiverRegion:this.model.receiverRegion});
            console.log(formData)
            httpAction(this.url.edit,formData,'put').then((res)=>{
              if(res.success){
                this.$message.success(res.message);
                this.$emit('ok');
              }else{
                this.$message.warning(res.message);
              }
            }).finally(() => {
              this.confirmLoading = false;
              this.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>
